define(function(require){
    var main =require('/static/main.js');
    require('/static/ajaxfileupload.js');
    var m = new main();
    function Cont(){
        this.Fun._init();
    }
    Cont.prototype.Fun = {
        SysVersionId:0,
        _init:function(){

            this.getlist();
            this.bindEvent();
        },
        bindEvent:function(){
        },
        getlist:function(){
            var _this = this;
            var emdataa = [];
            var param = {};
            var sort = 0;
            var table = $('#datatable').dataTable({
                'ordering':false,
                "serverSide": true,
                "searching":false,
                'bLengthChange': false,
                'bPaginate': true,
                "bDestory": true,
                "bRetrieve": true,
                "bFilter":false,
                "bSort": false,
                "bProcessing": true,
                "autoWidth": false,
                ajax: function (data, callback, settings) {
                    //封装请求参数
                    param.PageSize = data.length;//页面显示记录条数，在页面显示每页显示多少项的时候
                    param.start = data.start;//开始的记录序号
                    param.PageNo = (data.start / data.length)+1;//当前页码
                    sort = (param.PageNo-1)*param.PageSize;
                    //ajax请求数据
                    $.ajax({
                        type: "POST",
                        url: "/system/get-version-list",
                        cache: false,  //禁用缓存
                        data: param,  //传入组装的参数
                        dataType: "json",
                        success: function (result) {
                            var returnData = {};
                            returnData.draw = data.draw;
                            returnData.recordsTotal = result.total;
                            returnData.recordsFiltered = result.total;
                            returnData.data = result.data;
                            emdataa = result.data;
                            callback(returnData);
                        }
                    });
                },
                "aoColumns":[
                    {"mDataProp": 'SysVersionName'},
                    {"mDataProp": 'SysUseType',
                        render:function(data){
                            return  data == 100 ? '正常' : '试用';
                        }},
                    {"mDataProp": 'ExpiryDate',
                        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                            var ExpiryDate = '';
                            if(oData.SysUseType == 100){
                                ExpiryDate = oData.ExpiryDate+'年';
                            }else{
                                ExpiryDate = oData.ExpiryDate+'天';
                            }
                            $(nTd).html(ExpiryDate);

                        }},
                    {"mDataProp": 'Price'},
                    {"mDataProp": 'CreateTime',
                        render:function(data){
                            return m.formatTimeAll2(data);
                        }
                    },
                    {"mDataProp": 'Description'},
                    {
                        "mDataProp": "SysVersionId",
                        "sClass": "option_btn",
                        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                            // console.log(arguments);
                            $(nTd).html('<span class="btn_detial EditBtn" data-id="'+sData+'">编辑</span>');

                        }
                    }
                ]
            }).api();

            // 点击搜索
            $('#btnSearch').click(function(){
                param.SysVersionName = $('.SearchSysVersionName').val();
                var table = $('#datatable').DataTable();
                table.ajax.reload();

            });
            // 点击重置按钮
            $('#btnRefresh').click(function(){
                $('.SearchSysVersionName').val('');
                param.SysVersionName = '';
                var table = $('#datatable').DataTable();
                table.ajax.reload();
            });
            //编辑
            $(document).on('click','.EditBtn',function(){
                var SysVersionId = $(this).attr("data-id");
                $.each(emdataa,function(k,v){
                    if(SysVersionId == v.SysVersionId){
                        _this.SysVersionId = v.SysVersionId;
                        $("#Edit").modal("show");
                        $(".SysVersionName").val(v.SysVersionName);
                        $(".ExpiryDate").val(v.ExpiryDate);
                        $(".Price").val(v.Price);
                        $(".Description").val(v.Description);
                        if(v.SysUseType == 100){
                            $(".danwei").html('有效期（单位：年）');
                        }else{
                            $(".danwei").html('有效期（单位：天）');
                        }
                        return false;
                    }
                });

            });

            $('.add').click(function(){
                var $this = $(this);
                var data = {
                    SysVersionName:$('.SysVersionName').val().trim(),
                    ExpiryDate:$('.ExpiryDate').val().trim(),
                    Price:$(".Price").val().trim(),
                    Description:$(".Description").val().trim(),
                    SysVersionId:_this.SysVersionId
                }
                if(data.SysVersionName.length < 1){
                    layer.msg('请输入版本名称');
                    return false;
                }
                if(data.ExpiryDate.length < 1){
                    layer.msg('请输入有效期');
                    return false;
                }
                if(data.Price.length < 1){
                    layer.msg('请输入价格');
                    return false;
                }
               if(data.Description.length < 1){
                    layer.msg('请输入版本说明');
                    return false;
                }

                $.ajax({
                    type: "POST",
                    url: "/system/version-save",
                    data:data,
                    dataType: "json",
                    success: function (ret) {
                        if(ret.IsBizSuccess){
                                layer.msg('修改成功');
                            $('#Edit').modal('hide');
                            var table = $('#datatable').DataTable();
                            table.ajax.reload();
                        }else{
                            layer.msg(ret.ErrMsg);
                            return false;
                        }
                    }
                });
            })

            $(".Price").keyup(function () {
                var reg = $(this).val().match(/\d+\.?\d{0,2}/);
                var txt = '';
                if (reg != null) {
                    txt = reg[0];
                }
                $(this).val(txt);
            }).change(function () {
                $(this).keypress();
                var v = $(this).val();
                if (/\.$/.test(v))
                {
                    $(this).val(v.substr(0, v.length - 1));
                }
            });



            //VersionSave

        }

    }
    var a = new Cont();

})